package com.example.lotterysystem.service.activitystatus.Impl;

import com.example.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import com.example.lotterysystem.common.exception.ServiceException;
import com.example.lotterysystem.service.ActivityService;
import com.example.lotterysystem.service.activitystatus.ActivityStatusManager;
import com.example.lotterysystem.service.activitystatus.operater.AbstractActiviityOperater;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.impl.ActivityServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service
public class ActivityStatusManagerImpl implements ActivityStatusManager {
    @Autowired
    private final Map<String, AbstractActiviityOperater>operaterMap=new HashMap<>();

    @Autowired
    private ActivityService activityService;

    private final static Logger logger= LoggerFactory.getLogger(ActivityStatusManagerImpl.class);
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleEvent(ConvertActivityStatusDTO dto) {
        if(CollectionUtils.isEmpty(operaterMap)){
        logger.warn("operaterMap为空");
        return;
        }
        Map<String,AbstractActiviityOperater>currMap=new HashMap<>(operaterMap);
        Boolean update=false;

        //先处理人员
        update=processConvertStatus(dto,currMap,1);
        //再处理活动
        update=processConvertStatus(dto,currMap,2)||update;

        //更新缓存
        if(update){
        activityService.cacheActivity(dto.getActivityId());
        }

    }

    @Override
    public void rollbackHandlerEvent(ConvertActivityStatusDTO convertActivityStatusDTO) {
        for (AbstractActiviityOperater operater:operaterMap.values()) {
            operater.convert(convertActivityStatusDTO);
        }
        //更新缓存
        activityService.cacheActivity(convertActivityStatusDTO.getActivityId());

    }

    private Boolean processConvertStatus(ConvertActivityStatusDTO dto,
                                         Map<String, AbstractActiviityOperater> currMap,
                                         int sequence) {
        //遍历currMap
        Iterator<Map.Entry<String,AbstractActiviityOperater>>iterator=currMap.entrySet().iterator();

        Boolean update=false;
        while (iterator.hasNext()){
            AbstractActiviityOperater operater=iterator.next().getValue();
            //Operator是否需要转换
            if(operater.sequence()!=sequence
                    ||!operater.needConvert(dto)){
                continue;
            }
            //需要转换
            if(!operater.convert(dto)){
                logger.error("{}转换失败！",operater.getClass().getName());
                throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_CONVERT_ERROR);
            }
            //删除
            iterator.remove();
            update= true;
        }
return update;
    }


}
